package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.ConsultingDao;
import com.cn.demo4.model.ConsultingModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConsultingDaoImpl implements ConsultingDao {
    @Override
    public List<ConsultingModel> findAll() {
        String sql="select * from consulting order by id desc";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public List<ConsultingModel> getConsultingModelListForHome() {
        String sql="select * from consulting where status=1";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    private List<ConsultingModel> getData(ResultSet resultSet) {
        List<ConsultingModel> list = new ArrayList<>();
        try{
            while(resultSet.next()){
            ConsultingModel consultingModel = new ConsultingModel();
            consultingModel.setContent(resultSet.getString("content"));

            consultingModel.setStatus(resultSet.getInt("status"));
            consultingModel.setCreateTime(resultSet.getString("create_time"));
            consultingModel.setUpdateTime(resultSet.getString("update_time"));
            list.add(consultingModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
